/**
 * alert.js
 *-------------------------
 *
 * js base class
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from CouponMountain Ver3.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2006, Mezimedia. All rights reserved.
 *
 * @author     James.Huang <james_huang@mezimedia.com>
 * @copyright  (C) 2004-2006 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.0
 * @version    CVS: $Id: alert.js,v 1.8 2007/12/11 08:02:36 james_huang Exp $
 * @link       http://www.couponmountain.com/
 * @deprecated File deprecated in Release 3.0.0
 */

// for get scroll
var getPageScroll = function () {
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	return { xScroll:0, yScroll:yScroll };
}

// for get windows size
var getPageSize = function () {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	return { pWidth:pageWidth, pHeight:pageHeight, wWidth:windowWidth, wHeight:windowHeight };
}

// string buffer class
var StringBuffer = new Object();
StringBuffer.string = [];
StringBuffer.append = function (str) {
	this.string.push(str);
};
StringBuffer.clear = function () {
	this.string = [];
};
StringBuffer.out = function () {
	var out = this.string.join('');
	this.clear();
	return out;
};

// pop buffer class
var PopBox = new Object();
PopBox.initialize = function () {
	if (!document.getElementsByTagName) return;
	// generate required div
	body			= document.getElementsByTagName('body').item(0);
	al				= document.createElement('div');
	al.id			= 'popbox';
	al.className	= 'popbox';
	ol				= document.createElement('div');
	ol.id			= 'overlay';
	body.appendChild(al);
	body.appendChild(ol);
	// init variable for popbox class 
	this.load_path = '/images/loading.gif'
}
PopBox.pop = function (message) {
	// deal with message
	if (typeof(message) == 'object') {
		var popbox_message = message.innerHTML;
	} else {
		var popbox_message = message;
	}
	// get option form arguments 2
	var opt = Object.extend({
      form: '',
      focuson: '',
      redir_url: '',
	  loading: false
    }, arguments[1] || {});
	// set overlay div style
	$('overlay').style.width = getPageSize().pWidth + 'px';
	$('overlay').style.height = getPageSize().pHeight + 'px';
	$('overlay').style.display = 'block';
	// set popbox div content
	StringBuffer.append('<div id="popbox-mainbox">');
	StringBuffer.append('<table border="0" cellpadding="0" cellspacing="0" style="width:100%;text-align:center;">');
	StringBuffer.append('<tr><td class="poptop"><img src="/images/pop-close.png" onclick="PopBox.close(\''+ opt.form +'\', \''+ opt.focuson +'\', \''+ opt.redir_url +'\');" /></td></tr>');
	if (opt.loading) {
		var padding_top = (($('popbox').getStyle('height').toInt() - 20) / 2) - 45;
		StringBuffer.append('<tr><td style="padding-top:'+padding_top+'px;"><img src="'+this.load_path+'" style="vertical-align:middle" /></td></tr>');
		StringBuffer.append('<tr><td style="padding-top:15px;"><b>' + popbox_message + '</b></td></tr>')
	} else {
		StringBuffer.append('<tr><td><div id="popbox-content">' + popbox_message + '</div></td></tr></table>');
	}
	StringBuffer.append('</table></div>');
	$('popbox').innerHTML = StringBuffer.out();
	// set popbox div style
	var width = parseInt($('popbox').getStyle('width'));
	var height = parseInt($('popbox').getStyle('height'));
	var popX = getPageScroll().xScroll+((getPageSize().wWidth-width)/2)+'px';
	var popY = getPageScroll().yScroll+((getPageSize().wHeight-height)/2)+'px';
	$('popbox').style.left	= popX;
	$('popbox').style.top	= popY;
	$('popbox').style.display = 'block';

}

PopBox.close = function (form, focuson, redir_url) {
	if (redir_url) {
		window.location = redir_url;
	} else {
		$('popbox').style.display = 'none';
		$('overlay').style.display = 'none';
		// set now focus
		if (form && focuson) {
			eval("$('" + form + "')."+ focuson +".focus();");
		}
	}
}
/*
var onEnterKeyDown = function (e) {
	if ($('accountForm')) {
		CM.checkAccountForm();
	}
}

var onAccountFormFocus = function (e) {
	if ($('accountForm')) {
		regHotKey("Enter", onEnterKeyDown, {
			'type':'keypress',
			'propagate':false,
			'target':document,
			'reset':true
		});
	}
}
*/
PopBox.initialize();